package com.blank.task07;

import java.util.Scanner;

public class ChessBoardTry {
    //私有化成员变量黑白两种棋子
    private char black;
    private char white;
    //声明一个二维数组用来表示棋盘的容量
    private String[][] chessBoard;
    //私有化成员变量棋盘的行和列
    private int row;
    private int col;
    //无参构造方法
    public ChessBoardTry() {
    }
    //有参构造方法

    public ChessBoardTry(char black, char white, int row, int col) {
        setBlack(black);
        setWhite(white);
        setCol(col);
        setRow(row);
        setChessBoard(row,col);
    }

    public char getBlack() {
        return black;
    }

    public void setBlack(char black) {
        this.black = black;
    }

    public char getWhite() {
        return white;
    }

    public void setWhite(char white) {
        this.white = white;
    }

    public String[][] getChessBoard(int row,int col) {
        return chessBoard;
    }

    public void setChessBoard(int row,int col) {
       chessBoard =new String[row][col];
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }
    //棋盘的绘制
    public void getChessBoard(){
        for(int i=0;i<chessBoard.length;i++){
            for(int j=0;j<chessBoard[0].length;j++){
                if(0 == i){
                    chessBoard[i][j] = String.format("%x",j-1);
                }else if(0 == j){
                    chessBoard[i][j] = String.format("%x",i-1);
                }else{
                    chessBoard[i][j] = "+";
                }
            }
        }
        chessBoard[0][0] = " ";
    }

    //棋盘的打印
    public void printChessBoard(){
        for (int i=0;i<chessBoard.length;i++){
            for(int j = 0;j<chessBoard[0].length;j++){
                if("白".equals(chessBoard[i][j])){
                    System.out.print(white+" ");
                }else if("黑".equals(chessBoard[i][j])){
                    System.out.print(black+" ");
                }else {
                    System.out.print(chessBoard[i][j]+" ");
                }
            }
            System.out.println();
        }
    }
    //开始游戏
    public void gameStart(){
        Scanner sc = new Scanner(System.in);
        int x;
        int y;
        boolean flag = true;
        String chess;
        while(true){
            //五子棋比赛中一般黑棋先落子
            if(flag){
                System.out.println("请黑方落子");
                //棋子的类型
                chess = "黑";
            }else {
                System.out.println("请白方落字");
                chess = "白";
            }
            x = sc.nextInt()+1;
            y = sc.nextInt()+1;
            //判断两方落字是否符合规范
            if(x < 0 || y < 0 || x > (row -1) || y>(col-1)){
                System.out.println("落子已经超过棋盘范围");
                continue;
            }
            if("+".equals(chessBoard[x][y])){
                chessBoard[x][y] = chess;
            }else{
                System.out.println("该坐标已经被占用，请重新落子");
                continue;
            }
            //将棋盘打印
            printChessBoard();
            flag = !flag;
            boolean isWin = whichIsWin(getCol(),getRow(),x,y,chess);
            if (isWin) {
                System.out.printf("恭喜%s方获胜！", chess);
                break;
            }
        }
        sc.close();
    }
    //判断双方那方获胜
    private boolean whichIsWin(int col,int row,int x,int y,String chess){
        int sumOfRow = -1;
        int sumOfCol = -1;
        int sumOfLeftDiagonal = -1;
        int sumOfRightDiagonal = -1;
        int a;
        int b;
        //行中向左对齐的棋子
        a = x;
        b = y;
        while(true){
            if(chess.equals(chessBoard[a][b])){
                sumOfCol++;
                //防止左边的棋子越界
                if(0 == b){
                    break;
                }
                b--;
            }else{
                break;
            }
        }
        //行中向右对齐的棋子
        a = x;
        b = y;
        while(true){
            if(chess.equals(chessBoard[a][b])){
                sumOfCol++;
                //防止右边的棋子越界
                if(col-1 == b){
                    break;
                }
                b++;
            }else{
                break;
            }
        }
        //行中向上对齐的棋子
        a = x;
        b = y;
        while(true){
            if(chess.equals(chessBoard[a][b])){
                sumOfRow++;
                if(0 == row){
                    break;
                }
                a--;
            }else{
                break;
            }
        }
        //行中的向下对齐的棋子
        a = x;
        b = y;
        while(true){
            if(chess.equals(chessBoard[a][b])){
                sumOfRow++;
                if(row-1 == a){
                    break;
                }
                a++;
            }else{
                break;
            }
        }
        //左上角对齐的棋子
        a = x;
        b = y;
        while(true){
            if(chess.equals(chessBoard[a][b])){
                sumOfLeftDiagonal++;
                if(a == 0 || b == 0){
                    break;
                }
                a--;
                b--;
            }else{
                break;
            }
        }
        //右下角对齐的棋子
        a = x;
        b = y;
        while(true){
            if(chess.equals(chessBoard[a][b])){
                sumOfLeftDiagonal++;
                if(a == (row-1) || b == (col-1)){
                    break;
                }
                a++;
                b++;
            }else{
                break;
            }
        }
        //右上角对齐...
        // 右对角：右上数相同的棋子
        a = x;
        b = y;
        while (true) {
            if (chess.equals(chessBoard[a][b])) {
                sumOfRightDiagonal++;
                // 防止数组下标越界
                if (a == 0 || b == (col - 1)) {
                    break;
                }
                a--;
                b++;
            } else {
                break;
            }
        }
        //左下角对齐...
        a = x;
        b = y;
        while (true) {
            if (chess.equals(chessBoard[a][b])) {
                sumOfRightDiagonal++;
                // 防止数组下标越界
                if (b == 0 || a == (row - 1)) {
                    break;
                }
                a++;
                b--;
            } else {
                break;
            }
        }
        System.out.println("sumOfRow:"+sumOfRow);
        System.out.println("sumOfCol:"+sumOfCol);
        System.out.println("sumOfLeftDiagonal:"+sumOfLeftDiagonal);
        System.out.println("sumOfRightDiagonal:"+sumOfRightDiagonal);
        return (sumOfCol >=5 || sumOfRow >= 5 || sumOfLeftDiagonal >= 5 || sumOfRightDiagonal >= 5);
    }
}
